import {Food,Menu} from '../../models/shopping/food'
import ids from '../../prototype/ids'
import userinfo from "../../models/users/userInfo";
import shopModel from '../../models/shopping/shop'
import categoryModel from "../../models/shopping/category";
class Foods extends ids{
    constructor(){
        super()
        this.addFood = this.addFood.bind(this)
        this.addCategory = this.addCategory.bind(this)
        this.addCategory1 = this.addCategory1.bind(this)
    }
    async updateFood(req,res,next){
        // console.log(req.query)
        try{
            let {name, attributes, old_category,description, image_path, attrs, category_id, restaurant_id,item_id} = req.query
            if(!name){
                throw 'name出错'
            }else if(!restaurant_id || !Number(restaurant_id)){
                throw 'restaurant_id出错'
            }else if(!item_id || !Number(item_id)){
                throw 'item_id出错'
            }
            // console.log(category_id)
            let result = await Food.findOneAndUpdate({
                item_id,
                restaurant_id
            },{
                $set: {
                    name,
                    description,
                    image_path,
                    attrs,
                    category_id
                }
            },{new:true})
            if(old_category === category_id){
                // console.log(12312313)
            }else{
                let menu = await Menu.findOne({
                    id:old_category
                })
                var index = menu.foods.findIndex((item,index) => {
                    return item.item_id === Number(item_id)
                })
                menu.foods.splice(index,1);
                let menu1 = await Menu.findOne({
                    id:category_id
                })
                menu1.foods.push(result)
                await menu.save()
                await menu1.save()
            }
            // // console.log(menu.foods)
            //
            // console.log(1,menu)
            // await meun.save()
            // let menu1 = await Menu.findOne({
            //     id:category_id
            // })
            // console.log(2,menu1)
            // let food = await menu.findOne({
            //     item_id
            // })
            // console.log(food)
            // console.log(category_id)

            // let result = await Food.findOne({
            //     item_id,
            //     restaurant_id
            // }).update({
            //     name,
            //     description,
            //     image_path,
            //     attrs,
            //     category_id
            // })
            // console.log(result)
            res.send({
                status:1
            })
        }catch (e) {
            console.log(e)
            res.send({
                status:0
            })
        }

    }
    async addFood(req,res,next){
        try {
            var {name,description,image_path,activity,specs,restaurant_id,category_id,attributes} = req.query
            // console.log(req.query)
            let no = await this.setID('food_id');
            const rating_count = Math.ceil(Math.random() * 1000);
            const month_sales = Math.ceil(Math.random() * 1000);
            const tips = rating_count + '评分 月销量' + month_sales + '份'
            let meun;
            // console.log(no)
            let food = {
                item_id:Number(no),
                description,
                category_id,
                activity,
                name,
                tips,
                rating_count,
                month_sales,
                attributes,
                attrs:specs,
                rating:parseFloat(Math.random()*10).toFixed(2),
                restaurant_id,
                // category:"家常菜",
                image_path,
                specfoods:[
                    {
                        name:name,
                        food_id:no,
                        restaurant_id,
                        sku_id:no,
                        price:parseFloat(Math.random()*100,2),
                        item_id:no,
                    }
                ],
                recent_order_num: parseInt(Math.random()*1000)
            }
            try {
                meun = await Menu.findOne({
                    type:category_id
                })
            }catch (e) {
                res.send({
                    status:0,
                    message:'ERROR_GET_MEUN_INFO_FAILED'
                })
            }
            try {
                meun.foods.push(food)
                // console.log(meun)
                // meun.markModified("foods")
                await meun.save()
                await Food.create(food)
            }catch (e) {
                res.send({
                    status:0,
                    message:'ERROR_MEUN_OR_FOOD_SAVE_FAILED'
                })
            }
            res.send({
                status:1
            })
        }catch (e) {
            // console.log(e)
            res.send({
                status:0
            })
        }
    }
    async deleteFood(req,res,next){
        let {item_id,category_id} = req.query;
        try{
            let menu = await Menu.findOne({
                type:category_id
            })
            // console.log(category_id,menu)
            var index = menu.foods.findIndex((item,index) => {
                return item.item_id === Number(item_id)
            })
            menu.foods[index].is_featured = 1
            await menu.save()
            let result = await Food.findOneAndUpdate({
                item_id
            },{
                $set:{
                    is_featured:1
                }
            })
            res.send({
                status:1,
                message:"删除成功"
            })
        }catch (e) {
            console.log(e)
            res.send({
                status:0,
                message:"数据异常"
            })
        }

    }
    async getAllFood(req,res,next){
        let count = await Food.count({is_featured: 0});
        let {limit = 10,offset = 0} = req.query;
        // console.log(a)
        // console.log(req.query)
        var result = await Food.aggregate([
            {
                $match: {is_featured: 0}
            },{
                $lookup: {
                    from: "shops",
                    localField: "restaurant_id",
                    foreignField: "id",
                    as: "cate"
                }
            },{
                $lookup: {
                    from: "menus",
                    localField: "category_id",
                    foreignField: "id",
                    as: "category"
                }
            }
        ]).sort({restaurant_id  :1}).skip(Number(offset)).limit(Number(limit));
        res.send({
            count,
            result,
        // let result = await Food.find({},"-_id").sort({id:1}).limit(Number(limit)).skip(Number(offset));

            // a
        })
     }
    async addCategory(req,res,next){
        try {
            let name = ["好词","好吃","阿斯蒂","挖挖达瓦","去问我去饿","委屈饿嗡嗡","如无其他问题"]
            let no = await this.setID('category_id');
            let count = await shopModel.count()
            // let date = ['111111111','111155444','455464256','22222131213','2142141241','22213123','2012321314']
            // console.log(no)
            let menu = new Menu({
                description: "嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿",
                is_selected: true,
                name:name[parseInt(Math.random()*name.length)],
                id:  no,
                restaurant_id: parseInt(Math.random()*count),
                type: no
            })
            await menu.save()
            res.send({
                status:1
            })
        }catch (e) {
            console.log(e)
            res.send({
                status:0
            })
        }
    }
    async getFoodCategory(req,res,next){
        // console.log(req.query.restaurant_id)
        let result = await Menu.find({restaurant_id:Number(req.query.restaurant_id)})
        res.send({
            result
        })
    }
    async addCategory1(req,res,next){
        // console.log(req.query)
        // console.log(req.query.restaurant_id);
        let no = await this.setID('category_id');
        let result = new Menu({
            restaurant_id:Number(req.query.restaurant_id),
            description: req.query.description,
            is_selected: true,
            name:req.query.name,
            id:  no,
            type: no
        })
        await result.save()
        res.send({
            status:1
        })
    }
}
export default new Foods()